$('.select2').select2()
		getData(1);
		$('.search_btn1').click(function(){
			getData(1);
		})
		function getData(curr){
			var kind_text = $(".kind_sel").select2('val');
			$.ajax({
				type:"get",
				url:"https://www.retux.cn/MgProductSort/",
				async:true,
				data:{
					Content:kind_text,
					page:curr,
					row:10
				},
				success:function(data){
					if(data[0].length==0){
				  			layer.msg('暂无数据');
				  		}
					pageDivd(curr,data[1]);
					TableView(data[0]);
					$('.all_data').text(data[1])
				}
			});
		}
	    function pageDivd(curr,page_num){
	 	    	layui.use(['laypage', 'layer'], function(){
				  var laypage = layui.laypage
				  ,layer = layui.layer;
				    laypage.render({
				    elem: 'demo7'
				    ,count: page_num
				    ,limit:10
				    ,curr:curr
				    ,jump: function(obj,first){
				    	if(!first){
				    			getData(obj.curr);
				    		
				    	}
				    }
				  });
				})
	 	    }
	   //排序
	  function kindChange(num){
	  	//互换
	  	var kind_text = $(".kind_sel").select2('val');
	  	
	  	$('.order').click(function(){
	  		var kindLabel = $(this).attr('label');
	  		$.ajax({
	  			type:"get",
	  			url:"https://www.retux.cn/MgProductSorted",
	  			async:true,
	  			data:{
	  				Id:num,
	  				Kind:kindLabel,
	  				Content:kind_text
	  			},
	  			success:function(data){
	  				if(data=='success'){
	  					layer.msg('排序成功',{time:500},function(){
	  						location.reload();
	  					})
	  				}else{
	  					layer.msg('排序失败',{time:500})
	  				}
	  			}
	  		});
	  	})
	  	
	  	$('.order5').click(function(){
	  		var firseCheck,secondCheck;
	  		var check = $('input[type=checkbox]:checked');
	  		if(check.length==2){
	  			firseCheck = check.eq(0).attr('index');
	  			secondCheck = check.eq(1).attr('index');
	  			$.ajax({
	  				type:"get",
	  				url:"https://www.retux.cn/MgProductSwap",
	  				async:true,
	  				data:{
	  					Content:kind_text,
	  					Behind:secondCheck,
	  					First:firseCheck,
	  					
	  				},
	  				success:function(data){
	  					if(data == 'success'){
	  						layer.msg('交换成功',{time:500},function(){
	  							location.reload();
	  					    })
	  					}else{
	  						layer.msg('交换失败',{time:500})
	  					}
	  					
	  				}
	  			});
	  		}else{
	  			layer.msg('请选中两个商品')
	  		}
	  	})
	  	
	  	
	  }
	  function TableView(data){
	 	    	$('.table_data').html('');
	 	    	for (var i=0;i<data.length;i++) {
	 	    		var radio1,radio2,radio3;
	 	    		if(data[i][6].indexOf('新品')>-1){
	 	    			radio1 = '<input type="radio" checked="checked" disabled="disabled">';
	 	    		}else{
	 	    			radio1 = '<input type="radio" disabled="disabled">';
	 	    		}
	 	    		if(data[i][6].indexOf('热卖')>-1){
	 	    			radio2 = '<input type="radio" checked="checked" disabled="disabled">';
	 	    		}else{
	 	    			radio2 = '<input type="radio" disabled="disabled">';
	 	    		}
	 	    		if(data[i][6].indexOf('推荐')>-1){
	 	    			radio3 = '<input type="radio" checked="checked" disabled="disabled">';
	 	    		}else{
	 	    			radio3 = '<input type="radio" disabled="disabled">';
	 	    		}
	 	    		
	 	    		var trText = `<tr>
		      		    		<td><input  index="${data[i][0]}" type="checkbox"/></input></td>
		      		    		<td>${data[i][0]}</td>
		      		    		<td>${data[i][1]}</td>
		      		    		<td>
		      		    			<img src="${data[i][2]}" height="50"/>
		      		    		</td>
		      		    		<td>
		      		    			${data[i][3]}
		      		    		</td>
		      		    		<td>
		      		    			￥${data[i][4]}
		      		    		</td>
		      		    		<td>
		      		    			${data[i][5]}
		      		    		</td>
		      		    		<td>
		      		    			<div>
		      		    				<label class="label1">新品</label>${radio1}
		      		    			</div>
		      		    			<div>
		      		    				<label class="label2">热卖</label>${radio2}
		      		    			</div>
		      		    			<div>
		      		    				<label class="label3">推荐</label>${radio3}
		      		    			</div>
		      		    			
		      		    		</td>
		      		    		<td index="${data[i][0]}" style="width: 120px;">
		      		    			<div class="btn_done">
		      		    				<a class="look_detail" href="#">查看</a>
		      		    				<a class="order_by" href="#">排序</a>
		      		    			</div>
		      		    			<div>
		      		    				<a class="kind_del" href="#">此分类下删除商品</a>
		      		    			</div>
		      		    		</td>
		      		    	</tr>`;
		      		    	
		      		 $('.table_data').append(trText) ;  	
		      		    	
	 	    	}
	 	    	data_edtil();
	 	    	
	 	    }
	  function data_edtil(){
	  	$("#check_all").click(function(){
		 	    	if($(this).prop('checked')==true){
		 	    		
		 	    		$("tbody").find('input[type=checkbox]').prop('checked','checked')
		 	    	}else{
		 	    		$("tbody").find('input[type=checkbox]').prop('checked',false)
		 	    	}
		 	    })
	  	 $('.order_by').click(function(ev){
	  	 	var Number = $(this).parent().parent().attr('index');
			var ev = ev||event;
			$('.check_do').css('display','block')
			$('.check_do').css('left',ev.clientX+'px');
			$('.check_do').css('top',ev.clientY+'px');
			
			kindChange(Number);
			})
	  	 $('.look_detail').click(function(){
	  	 	var Index = $(this).parent().parent().attr('index');
	  	 	window.location.href="http://119.23.212.200:30000/product_details.html?Index="+Index;
	  	 })
	  	 $('.check_do').mouseleave(function(){
	  	 	$('.check_do').css('display','none');
	  	 })
	  	 $('.kind_del').click(function(){
	  	 			var kind_text = $(".kind_sel").select2('val');
	  	 			var checkNode = $("tbody").find('input[type=checkbox]:checked');
	 	    		var checkArr=[];
	 	    		var eventid = $(this).parent().parent().attr('index');
	 	    		if(checkNode.length>0){
		 	    		for (var i = 0;i<checkNode.length;i++) {
		 	    			var eventId = checkNode.eq(i).attr('index');
		 	    			checkArr.push(eventId);
		 	    		}
	 	    		}else{
	 	    			checkArr.push(eventid);
	 	    		}
	  	 	$.ajax({
	  	 		type:"get",
	  	 		url:"https://www.retux.cn/MgProductKindDel/",
	  	 		async:true,
	  	 		data:{
	  	 			Id:checkArr.join('-'),
	  	 			Kind:kind_text
	  	 		},
	  	 		success:function(data){
	  	 			if(data=='success'){
	  	 				layer.msg('删除成功',{time:500},function(){
	  	 					location.reload()
	  	 				})
	  	 			}else{
	  	 				layer.msg('删除失败',{time:500})
	  	 			}
	  	 		}
	  	 	});
	  	 })
	 }